var semestersForSelect = [];
var coursesForSelect = [];
var methodsForSelect = [];

/* JQuery additions */
function initFunctionalities() {
	setDatePickers();
	populateSemesterSelect();
	populateMethodSelect(true);
	populateCourseSelects();
}

/* Add options to select with null label (-choose-) or without it */
/* parameters: values are in JSON format */
function addOptionsToSelect(select, values, withNullLabel) {
	select.options.length = 0;
	if (withNullLabel) {
		select.options[0] = new Option(" ", " ");
	}
	for ( var j = 0; j < values.length; j++) {
		select.options.add(new Option(values[j].nameValue, values[j].key));
	}
}

/*Set JQuery date picker functionalities */
function setDatePickers() {
	var datepickersNumber = getAllElementChilds("formsList", "li").length;
	for ( var i = 0; i < datepickersNumber; i++) {
		$("#datepicker_" + i).datepicker();
		$("#datepicker_" + i).datepicker("option", "dateFormat", "dd.mm.yy");
	}
}

/*FUNCTIONS FOR POPULATING SELECTS WITH DATA*/

/**
 * SEMESTER SELECTS
 */
function populateSemesterSelect() {
	var select = document.getElementById("semesterSelect");
	getPossibleValuesForSemester();
	addOptionsToSelect(select, semestersForSelect, false);
}

function onSemesterChange() {
	populateCourseSelects();
}

function getPossibleValuesForSemester() {
	// Get values from DB - all semesters
	$.ajax({
		async : false,
		type : "POST",
		dataType : "text",
		url : "SemesterData"
	}).success(function(response) {
		semestersList = jQuery.parseJSON(response);
		semestersForSelect = semestersList.semesters;
	});
}

/**
 * METHOD SELECTS
 */
function populateMethodSelect(multiple) {
	if (!isInSessionStorage("methodsForSelect", JSON.stringify(methodsForSelect))) {
		getPossibleValuesForMethod();
		// If sessionStorage is supported by browser, add methods for select into it
		addToSessionStorage("methodsForSelect", JSON.stringify(methodsForSelect));
	}
	if (typeof (Storage) !== "undefined") {
		methodsForSelect = JSON.parse(getFromSessionStorage("methodsForSelect"));
	} 

	if (!multiple) {
		var select = document.getElementById("method");
		addOptionsToSelect(select, methodsForSelect, true);
		return;
	}
	var selectsNumber = getAllElementChilds("formsList", "li").length;
	for ( var i = 0; i < selectsNumber; i++) {
		var select = document.getElementById("methodSelect_" + i);
		addOptionsToSelect(select, methodsForSelect, true);
	}
}

function getPossibleValuesForMethod() {
	// Get values from DB - all learning methods
	$.ajax({
		async : false,
		type : "POST",
		dataType : "text",
		url : "LearningMethodData"
	}).success(function(response) {
		methodsList = jQuery.parseJSON(response);
		methodsForSelect = methodsList.methods;
	});
}

/**
 * COURSE SELECTS
 */
function populateCourseSelects() {
	var selectsNumber = getAllElementChilds("formsList", "li").length;
	getPossibleValuesForCourses();
	for ( var i = 0; i < selectsNumber; i++) {
		var select = document.getElementById("courseSelect_" + i);
		addOptionsToSelect(select, coursesForSelect, true);
	}
}

function getPossibleValuesForCourses() {
	// Get values from DB - all courses for current selected semester and
	// current user
	var semesterId = document.getElementById("semesterSelect").value;
	$.ajax({
		async : false,
		type : "POST",
		data : {
			"action" : "get",
			"semesterId" : semesterId,
		},
		dataType : "text",
		url : "CourseData"
	}).success(function(response) {
		coursesList = jQuery.parseJSON(response);
		coursesForSelect = coursesList.courses;
	});
}

// function getCodesAutocomplete() {
// var availableCodes = [ "MTAT.03.134", "MTAT.03.139", "MTAT.03.138",
// "MTAT.03.130", "MTAT.03.131", "MTAT.03.125" ];
// $("#codes").autocomplete({
// source : availableCodes
// });
// }
//
// function getCoursesAutocomplete() {
// var availableCourses = [ "Andmeturve", "Infosüsteemid", "Tarkvaraprojekt",
// "Tarkvaratehnika", "Tehisintellekt", "Keeletehnoloogia" ];
// $("#courses").autocomplete({
// source : availableCourses
// });
// }
